package cn.edu.zpark.security.springmvc.service.impl;

import cn.edu.zpark.security.springmvc.model.AuthenticationRequest;
import cn.edu.zpark.security.springmvc.model.UserDto;
import cn.edu.zpark.security.springmvc.service.AuthenticationService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @PackageName: cn.edu.zpark.security.springmvc.service.impl
 * @ClassName: AuthenticationServiceImpl
 * @Description:
 * @author: RZS
 * @date: 2021/7/20  15:02
 */
@Service
public class AuthenticationServiceImpl implements AuthenticationService {

    //用户信息
    private static Map<String,UserDto> userMap = new HashMap<>();

    static {
        Set<String> authorities1 = new HashSet<>(); authorities1.add("p1");
        Set<String> authorities2 = new HashSet<>(); authorities2.add("p2");
        userMap.put("zhangsan",new  UserDto("1010","zhangsan","123","张三","133443",authorities1));
                userMap.put("lisi",new   UserDto("1011","lisi","456","李四","144553",authorities2));
    }

    /**
     * 用户认证
     * @param authenticationRequest 用户认证请求
     * @return 认证成功的用户信息
     */
    @Override
    public UserDto authentication(AuthenticationRequest authenticationRequest) {
        if(authenticationRequest == null ||  StringUtils.isEmpty(authenticationRequest.getUsername()) || StringUtils.isEmpty(authenticationRequest.getPassword())){
            throw new RuntimeException("账号或密码为空");
        }

        // 模拟根据用户名查询用户数据
        UserDto userDto = getUserDto(authenticationRequest.getUsername());

        if(userDto == null){
            throw new RuntimeException("查询不到该用户");
        }
        // 验证密码
        if(!authenticationRequest.getPassword().equals(userDto.getPassword())){
            throw new RuntimeException("账号或密码错误");
        }

        return userDto;
    }

    //模拟用户查询
    public UserDto getUserDto(String username){
        return userMap.get(username);
    }
}
